/**
 *<p>Copyright: Copyright (c) 2012</p> 
 *<p>Company: SCPII</p>
 *<p>Project:scpii-api</p>
 *@author xiezhouyan
 *@Date 2013-3-7 上午10:00:32
 *@version 1.0
 *@description:
 */
package com.scpii.api.util;

import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import com.scpii.api.common.Constants;

public class RegisterVerifyUtil {

	private static final String ALL_NOT_ALLOW_KEY = "NO";

	public static Boolean verifyPhone(String phone, Map<String, String> configs) {
		if (configs == null) {
			return true;
		}
		String allowKey = configs.get(Constants.REGISTER_ALLOW_KEY);
		String allowArea = configs.get(Constants.REGISTER_ALLOW_AREA);

		String[] allowKeys = StringUtils.split(allowKey, ",");
		String[] allowAreas = StringUtils.split(allowArea, ",");
		if (allowKeys == null || allowKeys.length == 0) {
			return true;
		}
		if (ArrayUtils.contains(allowKeys, ALL_NOT_ALLOW_KEY)) {
			return false;
		}
		if (ArrayUtils.contains(allowKeys, Constants.PHONE_PROVIDER)) {
			if (allowArea != null) {
				if (StringUtils.startsWithAny(phone, allowAreas)) {
					return true;
				}
			} else {
				return true;
			}
		}
		return false;
	}

	public static Boolean verifyThirdParty(String thirdPartyKey, String allowKey) {
		if (!ArrayUtils.contains(Constants.THIRD_PARTY_USER_PROVIDER_ARRAY,
				thirdPartyKey)) {
			return false;
		}
		if (allowKey == null) {
			return true;
		}
		String[] allowKeys = StringUtils.split(allowKey, ",");
		if (allowKeys == null || allowKeys.length == 0) {
			return true;
		}
		if (ArrayUtils.contains(allowKeys, ALL_NOT_ALLOW_KEY)) {
			return false;
		}
		if (ArrayUtils.contains(allowKeys, thirdPartyKey)) {
			return true;
		}
		return false;
	}
}
